package com.kyzh.core.pager.gamedetail

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.atools.util.setHeight
import com.gushenge.core.beans.Pinglun
import com.gushenge.core.impls.GameImpl
import com.kyzh.core.R
import com.kyzh.core.activities.CommentDetailActivity
import com.kyzh.core.adapters.GameDetailBannerAdapter
import com.kyzh.core.databinding.ItemDynamicDiscussBinding
import com.kyzh.core.utils.TextViewExpandableAnimation
import com.kyzh.core.utils.ViewUtils.dp
import com.kyzh.core.utils.reviewImages
import com.kyzh.core.utils.setVisibility
import com.littlejerk.rvdivider.builder.XGridBuilder
import com.shuyu.gsyvideoplayer.GSYVideoManager
import kotlinx.android.synthetic.main.fragment_comment.*
import org.jetbrains.anko.support.v4.dip


class CommentFragment : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_comment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()

    }

    override fun onResume() {
        super.onResume()
        initData()
    }
    fun initData(){
        GameImpl. getPingLun{
            initPinglun(this)
        }
    }
    fun initView(){
    }
    private fun initPinglun(lb: ArrayList<Pinglun>?) {
        rvList.layoutManager = object : LinearLayoutManager(context){
        }
        val adapter = PinglunAdapter(lb)
          adapter.setOnItemClickListener { adapter, view, position ->
            CommentDetailActivity.start(requireContext(), lb?.get(position))
        }
        adapter.setEmptyView(R.layout.empty)
        rvList.adapter = adapter
        val inflate = LayoutInflater.from(requireContext()).inflate(R.layout.empty, null)
        inflate.post {
            inflate.setHeight(200.dp)
            val tv1 = inflate.findViewById<TextView>(R.id.tv1)
            tv1.text = "暂无评论"

        }
        adapter.setEmptyView(inflate)
        adapter.setOnItemClickListener { adapter, view, position ->
        }

    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    inner class PinglunAdapter(var lb: ArrayList<Pinglun>?): BaseQuickAdapter<Pinglun, BaseDataBindingHolder<ItemDynamicDiscussBinding>>(R.layout
        .item_dynamic_discuss,lb) {
        override fun convert(holder: BaseDataBindingHolder<ItemDynamicDiscussBinding>, item: Pinglun) {
            holder.dataBinding?.apply {

                //名字模块
                if(item.color.isNullOrEmpty()){
                    tvName.setTextSize(14f)
                    tvName .setHasColors(false,null)
                    tvName.setText(item.user_name)
                }else{
                    if(item.color.size == 1){
                        tvName.setTextSize(14f)
                        tvName.setText(item.user_name)
                        tvName .setHasColors(false,null)
                        tvName.setTextColor(Color.parseColor(item.color[0]))
                    }else{
                        var intArray = IntArray(item.color!!.size)
                        item.color?.forEachIndexed { index, s ->
                            intArray[index] = Color.parseColor(s)
                        }
                        tvName.setTextSize(14f)
                        tvName.setTextColor(Color.parseColor(item.color[0]))
                        tvName.setText(item.user_name)
                        tvName .setHasColors(true,intArray)
                    }
                }
                if(!item.video.isNullOrEmpty()){
                    rvList.setHeight(dip(160))
                    val bannerLayoutManager = object : GridLayoutManager(context, 1){
                        override fun canScrollHorizontally(): Boolean {
                            return false
                        }
                        override fun canScrollVertically(): Boolean {
                            return false
                        }
                    }
                    rvList.setVisibility(true)
                    var list = arrayListOf<String>()
                    if(item.images==null||item.images.size==0){
                        list.add(String())
                    }else{
                        list =  item.images
                    }
                    val ver =
                        XGridBuilder(requireContext()).setSpacing(3f)//这几个Spacing的优先级可看XGridBuilder说明
                            //这几个color和drawable的优先级可看XGridBuilder说明
                            .setColor(Color.BLUE)
                            //可设置颜色和drawable,drawable>color
                            .setHLineColor(Color.WHITE)
                            .setVLineColor(Color.WHITE)
                            //是否包括边界
                            .setIncludeEdge(false)
                            //竖直和水平分割线交叉处绘制的是竖直分割线颜色（交叉处属于竖直分割线）
                            .setVerticalIncludeEdge(true)
                    rvList.layoutManager = bannerLayoutManager
                    rvList.addItemDecoration(ver.build())
                    val gameDetailBannerAdapter = GameDetailBannerAdapter(R.layout.game_detail_banner_item1, list, item.video)
                    rvList.adapter = gameDetailBannerAdapter

                    gameDetailBannerAdapter.setOnItemClickListener { adapter, view, position ->
                        reviewImages(item.images, position)
                    }
                }else  if(item.images!=null&& item.images.size>0&& item.images[0] != "http:"&&item.images[0]!=""){

                    val bannerLayoutManager = object : GridLayoutManager(context, 6){
                        override fun canScrollHorizontally(): Boolean {
                            return false
                        }
                        override fun canScrollVertically(): Boolean {
                            return false
                        }
                    }
                    val value = object : GridLayoutManager.SpanSizeLookup() {
                        override fun getSpanSize(position: Int): Int {
                            return when (item.images.size) {
                                1 ->6
                                2 ->3
                                else -> 2
                            }
                        }
                    }

                    bannerLayoutManager.spanSizeLookup =value
                    rvList.setVisibility(true)
                    rvList.layoutManager = bannerLayoutManager
                    val ver =
                        XGridBuilder(requireContext()).setSpacing(3f)//这几个Spacing的优先级可看XGridBuilder说明
                            //这几个color和drawable的优先级可看XGridBuilder说明
                            .setColor(Color.BLUE)
                            //可设置颜色和drawable,drawable>color
                            .setHLineColor(Color.WHITE)
                            .setVLineColor(Color.WHITE)
                            //是否包括边界
                            .setIncludeEdge(false)
                            //竖直和水平分割线交叉处绘制的是竖直分割线颜色（交叉处属于竖直分割线）
                            .setVerticalIncludeEdge(true)
                    rvList.addItemDecoration(ver.build())
                    val gameDetailBannerAdapter = GameDetailBannerAdapter(R.layout.game_detail_banner_item1, item.images, item.video,false)
                    rvList.adapter = gameDetailBannerAdapter
                    if(item.images!=null &&item.images?.size==1){
                        rvList.setHeight(dip(125))
                    }else
                    if(item.images!=null &&item.images?.size==2){
                        rvList.setHeight(dip(95))
                    }else{
                        if(item.images?.size!!%3==0){
                            rvList.setHeight(dip(95)*(item.images?.size!!/3))
                        }else{
                            rvList.setHeight(dip(95)*(item.images?.size!!/3)+1)
                        }
                    }
                    gameDetailBannerAdapter.setOnItemClickListener { adapter, view, position ->
                        reviewImages(item.images, position)
                    }
                }else{
                    rvList.setVisibility(false)
                }
                data = item
                val view = holder.getView<TextViewExpandableAnimation>(R.id.tvContent)
                view.setText(item.content)
                view.resetState(true)
                if(!item.point.isNullOrEmpty()) {
                    rbStar.rating = item.point.toFloat()
                }
                val bannerLayoutManager1 = object :LinearLayoutManager(context, RecyclerView.VERTICAL,false){
                    override fun canScrollVertically(): Boolean {
                        return false
                    }
                }
                cilckView.setOnClickListener {
                    CommentDetailActivity.start(context,item)
                }
//                tvName.setOnClickListener {
//                    CommentDetailActivity.start(context,item)
//                }
//                rbStar .setOnClickListener {
//                    CommentDetailActivity.start(context,item)
//                    }
                rvPingLun.layoutManager = bannerLayoutManager1
                if(item.son_list!=null&&item.son_list.size>3){
                    tvPingLunAll.setVisibility(true)
                    val take = item.son_list.take(3)
                    item.son_list.clear()
                    item.son_list.addAll(take)
                }else{
                    tvPingLunAll.setVisibility(false)
                }
                if(item.son_list==null){
                    rvPingLun.setVisibility(false)
                }else{
                    rvPingLun.setVisibility(true)
                }
                val gameDetailPinglunAdapter =
                    GameDetailPinglunAdapter(item.son_list, item.user_name, item.id, item)
                rvPingLun.adapter = gameDetailPinglunAdapter
                gameDetailPinglunAdapter.setOnItemClickListener { _, _, _ ->
                    CommentDetailActivity.start(context,item)

                }
                tvPinglun.setOnClickListener {
                    CommentDetailActivity.start(context,item)
//                    GameCommentActivity.start(requireActivity(), SpConsts.gid,item.id,null,1,item)
                }
                tvPingLunAll.setOnClickListener {
                    CommentDetailActivity.start(context,item)
                }
                val drawable = resources.getDrawable(R.drawable.ic_zan)
                if(item.is_zan=="1"){
                    //点赞了
                    drawable.setTint(resources.getColor(R.color.colorPrimaryDark))
                    tvDianzan.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)
                }else{
                    //为点赞
                    drawable.setTint(Color.BLACK)
                    tvDianzan.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)
                }
                tvDianzan.setOnClickListener {
                    GameImpl.pingLunZan(item.id){

                        if(item.is_zan=="1"){
                            val i = item.zan.toInt() - 1
                            item.zan = i.toString()
                            item.is_zan ="0"
                        }else{
                            val i = item.zan.toInt() + 1
                            item.zan = i.toString()
                            item.is_zan ="1"

                        }
                        notifyItemChanged(holder.position)
                    }

                }
            }
        }
    }
}